package cn.wolfcode.luowowo.ticket.service.impl;

import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.ticket.domain.Payment;
import cn.wolfcode.luowowo.ticket.domain.PriceTicket;
import cn.wolfcode.luowowo.ticket.mapper.paymentMapper;
import cn.wolfcode.luowowo.ticket.service.IPaymentService;
import cn.wolfcode.luowowo.ticket.service.IPriceTicketService;
import cn.wolfcode.luowowo.util.AssertUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class PaymentServiceImpl implements IPaymentService {
    @Autowired
    private paymentMapper paymentMapper;

    @Reference
    private IPriceTicketService priceTicketService;

    @Reference
    private IUserInfoService userInfoService;

    @Override
    public void deleteByPrimaryKey(Long id) {
        paymentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Payment payment) {
        paymentMapper.insert(payment);
    }

    @Override
    public Payment selectByPrimaryKey(Long id) {
        return paymentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Payment> selectAll() {
        return paymentMapper.selectAll();
    }

    @Override
    public void updateByPrimaryKey(Payment payment) {
        paymentMapper.updateByPrimaryKey(payment);
    }

    @Override
    public void insetSubmitCreateOrder(Long ticketId, int num, UserInfo user,String msgCode,Date showVisitTimeInput)throws LogicException{
        AssertUtil.hasLength(msgCode, "验证码不能为空");
        Payment payment = new Payment();
        PriceTicket priceTicket = priceTicketService.selectByPrimaryKey(ticketId);
        int price = priceTicket.getPrice().intValue();
        payment.setNum(num);
        int discount = priceTicket.getDiscount().intValue();
        int tatol = (price - discount) * num;
        payment.setPaymentTime(new Date());
        payment.setPrice(new BigDecimal(tatol));
        payment.setTicketId(priceTicket);
        payment.setUserName(user.getNickname());
        payment.setFetchPhone(user.getPhone());
        payment.setUserId(user.getId());
        payment.setFetchTime(showVisitTimeInput);
        paymentMapper.insert(payment);
    }

    @Override
    public List<Payment> getByUserId(Long uid) {
        return paymentMapper.selectByUserId(uid);
    }
}
